<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
use Admin\Model\Goods_list;
class Goods_listController extends CommonController{
    private $model;
    public function __init(){
        //调用父类的构造
        parent::__init();
        //实例化商品列表模型
        $this->model = new Goods_list;
    }
    //首页
    public function index(){
        //获得商品的ID 
        $gid = Q('get.gid',0,'intval');
            //获取商品中所有的属性
        $tdata = Db::table("goods_attr")->join("type_attribute","type_attribute_taid","=","taid")->where("goods_gid={$gid} AND class=2")->orderBy("taid")->get();
        //设置空数组
        $taidAll=array();
        $tavalue=array();
        foreach ($tdata as $k=>$v){
                //获取所有的taid
            $taidAll[]=$v['taid'];
                //获取所有的商品中的属性
            $tavalue[$v['gaid']]=$v['gavalue'];
        }
            //去重后获取类型对应的taid
        $taid=array();
        $taid =array_unique($taidAll);
            //获取相同类型的属性
        $typeData=array();
        foreach($taid as $k=>$v){
            $typeData[]=Db::table('goods_attr')->where("type_attribute_taid={$v} AND goods_gid={$gid}")->orderBy("type_attribute_taid")->lists('gaid,gavalue');
        }
        View::with('typeData',$typeData);
            //找到taid对应的taname;
        $taname=array();
        foreach($taid as $k=>$v){
             $taname[] = Db::table("type_attribute")->where("taid={$v}")->pluck("taname");
        }
            //分配显示
        View::with('taname',$taname);
            //获取数据库里面的数据
        //获取数据的数量，分页
        $count = $this->model->where("goods_gid={$gid}")->count();
        //每10条数据分一页
        $page=Page::row(10)->make($count);
        $data =$this->model->limit(Page::limit())->where("goods_gid={$gid}")->get();
        // 获取data里面的combine字符串
        $spec = array();
        foreach ($data as $key => $value) {
                //获得组合属性的gaid
            $spec[] =$value['combine']; 
        }
        // 把gaid拆分成二维数组
        $ta = array();
        foreach($spec as $k=>$v){
                //把属性拆分成数组
            $ta[] = explode('|', $v);  
        }
        // 将得到的二维数组ta，压入到data对应的数组键值中，然后在进行转换对应的键值内容
        foreach($data as $kk=>$vv){
            $data[$kk]['gavalue']=$ta[$kk];
        }
        // 循环数据
        foreach ($data as $key=>$value){
            //把每条数据中的gavalue再循环
            foreach($data[$key]['gavalue'] as $km=>$vm){
                    //在data里面找gavalue，从$tavalue中获取对应的值；
                $data[$key]['gavalue'][$km]=$tavalue[$vm];
            }
        }
        View::with('page',$page);
        View::with('data',$data);
        View::make();
    }
    public function add(){
        if(IS_AJAX){
            if(!$this->model->store()){
                echo json_encode(array('status'=>0,'message'=>$this->model->getError()));
            }else{
                echo json_encode(array('status'=>1,'message'=>'添加成功'));
            }
            exit;
        }
    }
        //删除
    public function del(){
        //获取要删除的货品的ID
        $glid = Q('get.glid',0,'intval');
        //通过模型里面的方法删除
        if($this->model->del($glid)){
            View::success('删除成功');
        }

    }
        //编辑
    public function edit(){
        //获取要编辑的货品ID
        $glid = Q('get.glid',0,'intval');
            //获取gid
        $gid=Q('get.gid',0,'intval');
        if(IS_AJAX){
            if(!$this->model->edit($glid)){
                echo json_encode(array('status'=>0,'message'=>$this->model->getError()));
            }else{
                echo json_encode(array('status'=>1,'message'=>'修改成功'));
            }
            exit;
        }
            //获取taid和taname;
        $taid= Db::table("goods_attr")->join("type_attribute","type_attribute_taid","=","taid")->where("goods_gid={$gid} AND class=2")->lists("taid,taname");
            //获取相同类型的属性
        foreach($taid as $k=>$v){
            $typeData[]=Db::table('goods_attr')->where("type_attribute_taid={$k} AND goods_gid={$gid}")->lists('gaid,gavalue');
        }
        View::with('typeData',$typeData);
            //获取组合属性ID
        $str = $this->model->where("glid={$glid}")->pluck('combine');
            //把组合属性ID转为数组
        $combine = explode('|', $str);
            //把转化后的数组压入data中
        $data=$this->model->where("glid={$glid}")->get();
        $data[0]['combine'] = $combine;
            //把规格名压入数组
        foreach($taid as $K=>$v){
            $data[0]['taname'][]=$v;
        }
        View::with('data',$data[0]);
        View::make();
    }










}




?>